#include "mySort.h"
#include <iostream>
#include <string>

MySort::MySort()
{
}

MySort::MySort(int num, int *origin)
{
    this->num = num;
    this->origin = new int[num];
    for (int i = 0; i < num; i++)
    {
        this->origin[i] = origin[i];
    }
}

MySort::~MySort()
{

}

void MySort::changeSwap(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}

string MySort::printNum()
{
    string result = "";
    for (int i = 0; i < num; i++)
    {
        result += to_string(origin[i]) + " ";
    }
    return result;
}

string MySort::paritySort()
{
    bool oddSorted = false;
    bool evenSorted = false;

    while (!oddSorted || !evenSorted)
    {
        int base = 1;
        oddSorted = true;
        evenSorted = true;

        for (int i = base + 1; i < num; i+=2)
        {
            if (origin[i - 1] > origin[i])
            {
                changeSwap(origin[i - 1], origin[i]);
                oddSorted = false;
                cout << printNum() << endl;
            }
            
        }

        for (int i = base; i < num; i+=2)
        {
            if (origin[i - 1] > origin[i])
            {
                changeSwap(origin[i - 1], origin[i]);
                evenSorted = false;
                cout << printNum() << endl;
            }
            
        }
    }
    
    return "";
}

string MySort::bubbleSort()
{
    bool swapped;
    int start = 0;
    int end = num - 1;

    while (start < end)
    {
        swapped = false;

        // Pass from left to right (head to tail)
        for (int i = start; i < end; i++)
        {
            if (origin[i] > origin[i + 1])
            {
                changeSwap(origin[i], origin[i + 1]);
                // cout << "Pass " << start + 1 << ": " << printNum() << endl;
                cout << printNum() << endl;
                swapped = true;
            }
        }

        if (!swapped)
            break;

        swapped = false;

        // Pass from right to left (tail to head)
        for (int i = end - 1; i > start; i--)
        {
            if (origin[i] < origin[i - 1])
            {
                changeSwap(origin[i], origin[i - 1]);
                // cout << "Pass " << end << ": " << printNum() << endl;
                cout << printNum() << endl;
                swapped = true;
            }
        }

        if (!swapped)
            break;

        start++;
        end--;
    }

    return "";
}
